zxt.core.MESSAGETYPE = {};
zxt.core.MESSAGETYPE.WM_COMMAND_NEW = 11;
zxt.core.MESSAGETYPE.WM_COMMAND_EDIT = 12;
zxt.core.MESSAGETYPE.WM_COMMAND_DELETE = 13;
zxt.core.MESSAGETYPE.WM_COMMAND_SAVE = 14;
zxt.core.MESSAGETYPE.WM_COMMAND_CANCEL = 15;

/**
 * 执行操作消息, 操作列表: {@link zxt.type.Operation} 
 * @memberof
 */ 
zxt.core.MESSAGETYPE.WM_OPERATION_INVOKE = 20;

/**
 * 通知消息, 通知相关方支持的操作列表;
 * 操作列表: {@link zxt.type.Operation}  
 * @memberof
 */
zxt.core.MESSAGETYPE.WM_OPERATION_SUPPORT = 21;

/**
 * 通知消息, 通知模式的改变
 * @memberof
 */
zxt.core.MESSAGETYPE.WM_MODE_CHANGE = 31;

/**
 * @class 消息对象
 */
zxt.core.Message = function(type, data){
	
	/**
	 * 消息的类型
	 * @type int
	 */
	this.type = type;
	
	/**
	 * 消息的相关数据
	 * @type object
	 */
	this.data = data;
};

/**
 * 返回Message中的指定消息类型的键值内容。
 * @function
 * @return {string} "[TYPE:"+key+",DATA:" + this.data + "]"
 */
zxt.core.Message.prototype.toString = function(){
	for(var key in zxt.core.MESSAGETYPE){
		if (zxt.core.MESSAGETYPE[key]==this.type)
		return "[TYPE:"+key+",DATA:" + this.data + "]";
	}
};

/**
 * 操作按钮控件 继承于按钮控件
 * @class 操作按钮控件类
 * @param {string} label 按钮的标签
 * @param {string} [text] 按钮的显示文本, 未指定时使用传入的标签作为显示
 * @param {zxt.type.Operation.*} opt 操作类型
 * @param {zxt.ui.Widget} target 操作的目标
 * 
 */
zxt.ui.OptButton = function(label, text, opt, target){
	zxt.ui.Button.call(this, label, text);
	this._opt = opt;
	this._target = target;
	target.addAttachButton(this);
};

/**
 * 根据传入的DOM元素, 解析传入的相关属性, 创建相应的按钮对象
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @return {zxt.ui.OptButton} 根据传入的DOM元素创建的按钮对象
 */
zxt.ui.OptButton.parse = function(elem){
	var j = $(elem);

	var _label = j.attr("zxt.label");
	var _text = j.attr("zxt.text");		
	var _opt = j.attr("zxt.opt");		
	var _targetStr = j.attr("zxt.target");			
	var _target = zxt.Page.INSTANCE.getComponent(_targetStr);
	var _instance = new zxt.ui.OptButton(_label, _text, _opt, _target);
	return _instance;
};

zxt.defineClazz(zxt.ui.OptButton, zxt.ui.Button);

/**
 * 返回当前按钮操作类型
 * @return {zxt.type.Operation.*} 操作类型
 */
zxt.ui.OptButton.prototype.operation = function(){
	return this._opt;
};

/**
 * 操作按钮单击事件 向目标发送一条{zxt.core.Message}的消息
 */
zxt.ui.OptButton.prototype.onClick = function(){
  //this.enable(false);
	this._target.onMessage(new zxt.core.Message(zxt.core.MESSAGETYPE.WM_OPERATION_INVOKE, {opt:this._opt}));	
};

/**
 * 按钮控件消息响应函数
 * @param {zxt.core.Message} msg 传递的消息对象
 */
zxt.ui.OptButton.prototype.onMessage = function(msg){
	switch(msg.type){
		case zxt.core.MESSAGETYPE.WM_OPERATION_SUPPORT:
			this.enable(false);
			var _opts = msg.data.opts;
			for( var i=0; i<_opts.length; i++ ){
				if ( _opts[i] == this._opt ){
					this.enable(true);
				}	
			}	
			break; 
		default:
	}	
};

/**
 * 工具栏类
 * @class 工具栏类
 * @param {Object} target 操作的目标
 * @param {Map} texts 操作按钮的描述 不传的话默认使用zxt.cfg.OperationText的配置
 */
zxt.ui.OptBar = function(target, texts){
	zxt.ui.Bar.call(this);
	
	this._$tail = $("<td/>").width("100%");
	this._$tr = $("<tr/>").append(this._$tail);	
	this._$body = $("<tbody/>").append(this._$tr);	
	this._$table = $("<table border=0 cellspacing=0 cellpadding=0 />").append(this._$body);
			
	this._last = null;
	this._defaultTarget = target;
	this._texts = texts || zxt.cfg.OperationText;
};

/**
 * 根据传入的DOM元素, 解析传入的相关属性, 创建相应的按钮对象
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @return {zxt.ui.OptBar} 根据传入的DOM元素创建的按钮对象
 */
zxt.ui.OptBar.parse = function(elem){
	var j = $(elem);	
	var _targetStr = j.attr("zxt.target");
	var _target = zxt.Page.INSTANCE.getComponent(_targetStr);	
	if ( zxt.util.isAvailable(_targetStr) ){				
		if ( !zxt.util.isAvailable(_target) ){		
			throw new Error("No Component with ID "+_targetStr);
		}
	};
	var _instance = new zxt.ui.OptBar(_target);	
	var _default = j.attr("zxt.default");
	if ( _default == "true" ){
		_instance.addDefaultButtons();
	}		
	
	j.children().each( function( idx, dom )
		{
			_instance.addElement(dom);
		}
	);	
	
	return _instance;
};

zxt.defineClazz(zxt.ui.OptBar, zxt.ui.Bar);

/**
 * <p>
 * 返回按钮的控件类型
 * </p>
 * 
 * @function
 * @returns {string} "zxt.ui.OptBar"
 */
zxt.ui.OptBar.prototype.getType = function(){
  return "zxt.ui.OptBar";
};

/**
 * 工具栏新增按钮方法
 * @function
 * @param {zxt.ui.Button} button 按钮控件对象
 */
zxt.ui.OptBar.prototype.addButton = function (button){
	var _elem = button.render();
	this.addElement(_elem);	
};

/**
 * 根据目标支持的操作添加默认的操作按钮
 * @function
 * @param {zxt.ui.Form/zxt.ui.DataGrid} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addDefaultButtons = function (target){
	var _target = target || this._defaultTarget;
	var _opts = _target.getSupportOperations();
	for( var i = 0 ; i < _opts.length; i++ ){
		var _opt = _opts[i];
		var _text = this._texts[_opt] || _opt;
		var _button = new zxt.ui.OptButton(_opt, _text, _opt, _target);	
		this.addButton(_button);
	}
};

/**
 * 添加新建按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addNewButton = function (target){
	var _target = target || this._defaultTarget;
	var _button = new zxt.ui.OptButton(zxt.type.Operation.CREATE,"新建", zxt.type.Operation.CREATE, _target);	
	this.addButton(_button);
};

/**
 * 添加编辑按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addEditButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.EDIT,"编辑", zxt.type.Operation.EDIT, _target);	
	this.addButton(_button);
};

/**
 * 添加删除按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addDeleteButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.DELETE,"删除", zxt.type.Operation.DELETE, _target);	
	this.addButton(_button);	
};	

/**
 * 添加保存按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addSaveButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.SAVE,"保存", zxt.type.Operation.SAVE, _target);	
	this.addButton(_button);	
};

/**
 * 添加取消按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addCancelButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.CANCEL,"取消", zxt.type.Operation.CANCEL, _target);	
	this.addButton(_button);		
};

/**
 * 添加查询按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addQueryButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.QUERY,"查询", zxt.type.Operation.QUERY, _target);	
	this.addButton(_button);		
};

/**
 * 添加重置按钮
 * @function
 * @param {zxt.ui.Widget} target 操作的对象目标
 */
zxt.ui.OptBar.prototype.addResetButton = function (target){
	var _target = target || this._defaultTarget;	
	var _button = new zxt.ui.OptButton(zxt.type.Operation.RESET,"重置", zxt.type.Operation.RESET, _target);	
	this.addButton(_button);		
};